<?php

namespace App\Web\Controller;

use App\Web\Facades\UserAuth;
use App\Web\Model\OperatorLog;
use App\Web\Service\RightCheck;
use EchoPHP\Facades\Db;
use EchoPHP\Http\Request;
use App\Web\Model\Team as TeamModel;


class Team extends Controller {

    public $rightCheck = [
        'index' => RightCheck::RIGHT_TEAM_ALL,
        'add' => RightCheck::RIGHT_TEAM_ALL,
        'edit' => RightCheck::RIGHT_TEAM_ALL,
        'delete' => RightCheck::RIGHT_TEAM_ALL,
    ];

    /**
     * 获取团队信息，用于团队管理页面
     * @param Request $request
     * @return \EchoPHP\Http\Response\JsonResponse
     * @throws \Exception
     */
    public function index(Request $request) {
        $teamModel = new TeamModel();
        $teamInfo = $teamModel->getTeamList();
        $isSuper = UserAuth::getLoginInfo('issuper', '');
        $teamInfo['issuper'] = $isSuper;
        return $this->json($teamInfo);
    }

    /**
     * 获取团队下各个角色成员数目
     * @param Request $request
     * @return \EchoPHP\Http\Response\JsonResponse
     * @throws \Exception
     */
    public function analysisTeamUser(Request $request) {
        $teamCode = $request->getParam('teamcode', '');
        $teamModel = new TeamModel();
        $userCount = $teamModel->analysisTeamUserByRole($teamCode);
        return $this->json($userCount);
    }

    /**
     * 获取团队列表，用于团队选择框
     * @param Request $request
     * @return \EchoPHP\Http\Response\JsonResponse
     * @throws \Exception
     */
    public function cleanTeamList(Request $request) {
        $isSelection = $request->getParam('isselection', false); //是否是查看团队数据，不对数据进行操作；要对数据进行操作的时候按团队层级显示数据，不操作数据则能查看全部数据
        $defaultTeam = $request->getParam('defaultteam', false); //是否是查看 默认团队数据
        $teamModel = new TeamModel();
        $teamInfo = $teamModel->getCleanList($isSelection, $defaultTeam);
        return $this->json($teamInfo);
    }

    /**
     * 新增团队
     * @param Request $request
     * @return \EchoPHP\Http\Response\JsonResponse
     * @throws \Exception
     */
    public function add(Request $request) {
        $parantCode = $request->getPost('parentcode/s'); //上级团队code
        $teamName = $request->getPost('teamname'); //团队名称

        $teamModel = new TeamModel();
        $executeResult = $teamModel->addTeam($parantCode, $teamName);
        return $this->json($executeResult);
    }

    /**
     * 修改团队名称
     * @param Request $request
     * @return \EchoPHP\Http\Response\JsonResponse
     * @throws \Exception
     */
    public function edit(Request $request) {
        //获取提交参数
        $teamCode = $request->getPost('teamcode/s');
        $teamName = $request->getPost('teamname'); //团队名称

        $teamModel = new TeamModel();
        $executeResult = $teamModel->editTeam($teamCode, $teamName);
        return $this->json($executeResult);
    }

    /**
     * 删除团队
     * @param Request $request
     * @return \EchoPHP\Http\Response\JsonResponse
     * @throws \Exception
     */
    public function delete(Request $request) {
        $teamCode = $request->getPost('teamcode/s');

        $teamModel = new TeamModel();
        $executeResult = $teamModel->deleteTeam($teamCode);
        return $this->json($executeResult);
    }

    /** 下一级团队与成员
     * @param Request $request
     * @return \EchoPHP\Http\Response\JsonResponse
     * @throws \Exception
     */
    public function detail(Request $request) {
        $teamCode = $request->getParam('teamcode/s', '');
        $teamModel = new TeamModel();
        $result = $teamModel->detail($teamCode);
        return $this->json([
            'status' => true,
            'message' => '获取成功',
            'teamtree' => $result
        ]);
    }

    /**
     * 获取用户能查看的团队列表
     * @param Request $request
     */
    public function teamList(Request $request) {
        $isLeader = UserAuth::ckhTeamLeader();
        $isSuper = UserAuth::ckhSuperLeader();
        $teamCode = UserAuth::getLoginInfo('teamcode');
        // 超管是否获取全部团队列表
        $getAll = $request->getParam('supergetall', 1);
        if (!$isLeader && !$isSuper) {
            $teamList = [];
        } else {
            if ($isSuper && $getAll) {
                $teamCode = '';
            }
            $teamModel = new TeamModel();
            $teamList = $teamModel->getFollowTeamList($teamCode);
        }
        return $this->json([
            'status' => true,
            'message' => '获取成功',
            'teamtree' => $teamList
        ]);
    }
}
